package com.rao.travel.controller;

import com.github.pagehelper.PageInfo;
import com.rao.travel.pojo.Product;
import com.rao.travel.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.security.RolesAllowed;
import java.util.List;

/**
 * @author Srao
 * @version 1.0
 * @className ProductController
 * @date 2019/10/25 21:57
 * @package com.rao.travel.controller
 * @description
 */
@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    /*查询所有商品--不分页*/
    /*@RequestMapping("/findAll.do")
    public ModelAndView findAll() throws Exception {
        ModelAndView modelAndView = new ModelAndView();
        List<Product> productList = productService.findAll();
        modelAndView.addObject("productList", productList);
        modelAndView.setViewName("product-list");
        return modelAndView;
    }*/

    /*分页查询所有商品*/
    @RequestMapping("/findAll.do")
    @RolesAllowed("ROLE_ADMIN")//只有管理员才可以查看所有商品信息
    public ModelAndView findAll(
            @RequestParam(name = "page", required = true, defaultValue = "1")Integer page,
            @RequestParam(name = "pageSize", required = true, defaultValue = "5")Integer pageSize
    ) throws Exception {
        ModelAndView modelAndView = new ModelAndView();
        List<Product> productList = productService.findAll(page, pageSize);
        PageInfo pageInfo = new PageInfo(productList);
        modelAndView.addObject("pageInfo", pageInfo);
        modelAndView.setViewName("product-list");
        return modelAndView;
    }

    /*保存商品*/
    @RequestMapping("save.do")
    public String save(Product product) throws Exception {
        productService.save(product);
        return "redirect:findAll.do";
    }

}
